<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程数据添加</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->

      <!-- 用户列表区域 -->
        <!-- 内容主体区域 -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" width="50%"  border stripe>
          <el-form-item label="课程名称" prop="course_name">
            <el-input v-model="addForm.course_name" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="课程介绍" prop="introduce">
            <el-input v-model="addForm.introduce" style="width: 600px"></el-input>
          </el-form-item>
          <el-form-item label="幼儿要求" prop="requirement">
            <el-select v-model="addForm.requirement" placeholder="幼儿要求">
              <el-option
                v-for="item in requirement"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="合作单位" prop="cooperation_id">
            <el-select v-model="addForm.cooperation_id" placeholder="合作单位">
              <el-option
                v-for="item in coursenameList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="基本费(元)" prop="teach_fee">
            <el-input v-model="addForm.teach_fee" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="材料费(元)" prop="material_fee">
            <el-input v-model="addForm.material_fee" style="width: 200px"></el-input>
          </el-form-item>
        </el-form>
      <!-- 分页区域 -->
    </el-card>

    <el-row :gutter="20" >
      <el-col :span="4" >
        <el-button type="primary" @click="addCouser()">添加课程数据</el-button>
      </el-col>
    </el-row>

  </div>
</template>

<script>
    export default {
        data() {
            return {
                // 添加用户的表单数据
                addForm: {
                    course_name: '',
                    introduce: '',
                    requirement: '',
                    teach_fee:'',
                    material_fee:'',
                    cooperation_id:''
                },
                requirement: [{
                    value: '大班',
                    label: '大班'
                }, {
                    value: '中班',
                    label: '中班'
                },{
                    value: '小班',
                    label: '小班'
                }
                ],
                // 添加表单的验证规则对象
                addFormRules: {
                    course_name: {
                        required: true,
                        message: '请输入课程名字',
                        trigger: 'blur'
                    },
                    introduce: {
                        required: true,
                        message: '请输入介绍',
                        trigger: 'blur'
                    },
                    requirement: {
                        required: true,
                        message: '请输入邮箱',
                        trigger: 'blur'
                    },
                    teach_fee: {
                        required: true,
                        message: '请输入教学费用',
                        trigger: 'blur'
                    },
                    material_fee: {
                        required: true,
                        message: '请输入材料费用',
                        trigger: 'blur'
                    },
                    cooperation_id: {
                        required: true,
                        message: '请输入',
                        trigger: 'blur'
                    },

                },
                coursenameList:[]
                
        }
        },
        created() {
            this.getConstitutionList()
        },
        methods: {
           async getConstitutionList() {
             const { data: res } = await this.$http.get('course/name')

             if (res.code !== 200) {
                 return this.$message.error('获取用户列表失败！')
             }
             this.coursenameList = res.data
           },
            // 点击按钮，添加新用户
            addCouser() {
                console.log(this.addForm)
                let form = new FormData()
                form.append('course_name',this.addForm.course_name)
                form.append('introduce',this.addForm.introduce)
                form.append('requirement',this.addForm.requirement)
                form.append('teach_fee',this.addForm.teach_fee)
                form.append('material_fee',this.addForm.material_fee)
                form.append('cooperation_id',this.addForm.cooperation_id)


                this.$refs.addFormRef.validate(async valid => {
                    if (!valid) return
                            const { data: res } = await this.$http.post('course/', form)
                            if (res.code !== 200) {
                              this.$message.error('添加用户失败！')
                            }

                    this.$message.success('添加用户成功！')
                })
            }
        }
    }
</script>

<style lang="less" scoped>
  .el-col {
    margin-top: 30px;
    float:right;
  }
</style>
